package handler;

import entity.CourseDO;
import entity.StudentDO;
import service.CourseService;
import service.StudentService;
import service.impl.CourseServiceImpI;
import service.impl.StudentServiceImpI;
import view.CourseView;
import view.MainView;
import view.UpdateCoursView;
import view.UpdateStudentView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateCourseViewHandler implements ActionListener {

    private UpdateCoursView updateCoursView;
    private CourseView courseView;

    public UpdateCourseViewHandler(UpdateCoursView updateCoursView, CourseView courseView) {
        this.updateCoursView = updateCoursView;
        this.courseView = courseView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("修改".equals(text)) {
            CourseService courseService = new CourseServiceImpI();
            CourseDO courseDO = updateCoursView.buildUpdateCourseDO();
            boolean updateResult = courseService.update(courseDO);
            if (updateResult) {
                //重新加载表格
                courseView.reloadTable();
                updateCoursView.dispose();
            } else {
                JOptionPane.showMessageDialog(updateCoursView, "修改失败");
            }
        }
    }
}
